<?php

namespace controller;

use page\PageParam;
use service\ToolsService;
use think\exception\HttpResponseException;
use think\Response;
use think\Validate;

/**
 * 基础接口类
 * Class BasicApi
 * @package controller
 */
class BasicApi{

    /**
     * 当前请求对象
     * @var \think\Request
     */
    protected $request;
    public    $model    = null;
    public    $table    = '';
    public    $NotFound = 404;

    /**
     * 构造方法
     * BasicApi constructor.
     */
    public
    function __construct(){
        ToolsService::corsOptionsHandler();
        $this->request = app('request');
        //        try {
        //            ApiService::write();
        //        } catch (\Exception $e) {
        //            $this->error($e->getMessage(), $e->getCode());;
        //        }
    }

    /**
     * 返回成功的操作
     * @param mixed $msg 消息内容
     * @param array $data 返回数据
     * @param integer $code 返回代码
     */
    protected
    function success($msg = '', $data = []){
        $this->return_json(200, $msg, $data);
    }

    /**
     * 返回失败的请求
     * @param mixed $msg 消息内容
     * @param array $data 返回数据
     * @param integer $code 返回代码
     */
    protected
    function error($msg, $code = 201, $data = []){
        $code<200 && $code += 200;
        ToolsService::error($msg, $data, $code);
    }

    /**
     * 返回资源不存在
     * @param integer $code 返回代码
     */
    protected
    function notAllow($code = 403){
        ToolsService::error('', [], $code);
    }

    /**
     * 返回资源不存在
     * @param integer $code 返回代码
     */
    protected
    function notFound($code = 4041){
        ToolsService::error('', [], $code);
    }


    /**
     * 获得请求参参数
     * // Created by William Wang<wxx@ruitukeji.com>
     * // Created at 2018-10-15 10:45:12
     */
    protected
    function get_req_params($keys = [], $rule = false){
        $params = input("param.");
        $ret    = [];
        if(empty($keys)){
            return $params;
        }
        foreach($keys as $k => $v){
            if(is_numeric($k)){ // 一维数组
                $ret[$v] = array_key_exists($v, $params) ? $params[$v] : '';
                continue;
            }
            $ret[$k] = array_key_exists($k, $params) ? $params[$k] : (empty($v) ? '' : $v);
        }

        if($rule){
            $this->validate_params($ret, $rule);
        }
        return $ret;
    }

    /**
     * // 根据规则检查请求参数
     * // Created by William Wang<wxx@ruitukeji.com>
     * // Created at 2018-10-15 11:26:08
     * @param array $params
     * @param array $rule
     * @return bool
     *
     */
    function validate_params($params = [], $rule = []){
        if(empty($params)){
            $this->return_json(4001, '缺少必要参数.');
        }
        if(empty($rule)){
            foreach($params as $k => $v){
                $rule[$k] = 'max:250';
            }
        }
        //exit(print_r($rule));
        $validate = new Validate($rule);
        if($validate->check($params)){
            return true;
        }
        $this->return_json(4002, '', $validate->getError());
    }

    function get_page_param(){
        $page_param           = new PageParam();
        $page_param->page     = intval(input('page', 1));
        $page_param->pageSize = intval(input('pageSize', 10));
        $page_param->orderBy  = input('orderBy', 'id');
        $page_param->order    = input('order', 'DESC');
        return $page_param;
    }

    /**
     * 返回成功的操作
     * @param mixed $msg 消息内容
     * @param array $data 返回数据
     * @param integer $code 返回代码
     */

    function return_json($result = 0, $msg = '', $data = [], $is_log = false){
        $ret_arr = return_array($result, $msg, $data);
        if($is_log){
            trace('returnJson : ');
            trace(json_encode($ret_arr));
        }
        throw new HttpResponseException(Response::create($ret_arr, 'json', 200, ToolsService::corsRequestHander()));
    }


}
